package com.caishi.lkx.common.aliyun;

import com.aliyun.teaopenapi.models.Config;
import com.aliyun.vod20170321.Client;
import com.aliyun.vod20170321.models.*;
import com.zzw.common.AliyunConfig;
import com.zzw.common.video.AliyunVideoConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;


/**
 * @author lkx
 * &#064;date  2024/5/23
 * &#064;description  阿里云视频类扩展
 */
@Slf4j
@Primary
@Service
public class AliyunVideoPluginsExt {


    protected Client client;

    @Autowired
    private AliyunVideoConfig videoConfig;

    @Autowired
    private AliyunConfig aliyunConfig;

    @SneakyThrows
    private synchronized Client createClient() {
        if (client == null) {
            client = new Client(new Config().setAccessKeyId(aliyunConfig.getAccessKeyId()).setAccessKeySecret(aliyunConfig.getAccessKeySecret()).setEndpoint(videoConfig.getEndpoint()));
        }
        return client;
    }

    /**
     * 获取阿里云视频列表
     */
    public GetVideoListResponse getVideoList(GetVideoListRequest videoInfo) throws Exception {

        return this.createClient().getVideoList(videoInfo);
    }


    /**
     * 获取分类
     */
    public GetCategoriesResponse getCategories(long cateId) throws Exception {
        GetCategoriesRequest request = new GetCategoriesRequest();
        request.setCateId(cateId);
        request.setPageSize(100L);
        return this.createClient().getCategories(request);
    }

    /***
     * 获取视频播放凭证
     * @param videoId 视频id
     * @return
     * @throws Exception
     */
    public String getVideoPlayAuth(String videoId) throws Exception {

        GetVideoPlayAuthRequest getVideoPlayAuthRequest = new GetVideoPlayAuthRequest();
        getVideoPlayAuthRequest.setVideoId(videoId);
        getVideoPlayAuthRequest.setAuthInfoTimeout(3000L);

        return this.createClient().getVideoPlayAuth(getVideoPlayAuthRequest).getBody().getPlayAuth();
    }
    /**
     * 获取url，7.0.3版本集成ui只能传url来播放
     * */
    public GetPlayInfoResponseBody.GetPlayInfoResponseBodyVideoBase getPlayInfo(String videoId) throws Exception {
        GetPlayInfoRequest request=new GetPlayInfoRequest();
        request.setVideoId(videoId);
        request.setFormats("");
        request.setAuthTimeout(3000L);
        GetPlayInfoResponseBody.GetPlayInfoResponseBodyPlayInfoList playInfoList = this.createClient().getPlayInfo(request).getBody().getPlayInfoList();
        return this.createClient().getPlayInfo(request).getBody().getVideoBase();
    }

}
